<template>
	<div class="content">
		<view class="cu-bar bg-white search fixed" :style="[{top:CustomBar + 'px'}]">
			<view class="search-form round">
				<input type="text" placeholder=" 搜索" confirm-type="search" @input="searchIcon" disabled="true" placeholder-class="cuIcon-search logo" @click="search"></input>
			</view>
		</view>
		<view   style="margin-top: 100upx;"  :class="[control == 0?'':'show']"> 
			<date :animation="animationData" :url="url1" :getdate = "getdate" @reachdate = "reachdate"></date>
		</view>
		<view  style="margin-top: 100upx;" :class="[control == 1?'':'show']">
		    <recommend :animation="animationData" :getrecommend = "getrecommend" @reachrecommend = "reachrecommend"></recommend>
		</view>
		<view :animation="animationData" style="margin-top: 100upx;" :class="[control == 2?'':'show']">
			<attention></attention>
		</view>
	</div>
</template>

<script>
    import {
        mapState
    } from 'vuex'
	import date from "@/component/date/date.vue"
	import recommend from "@/component/recommend/recommend.vue"
	import attention from "@/component/attention/attention.vue"
    import {WebSocket} from '../../js/websocket-uni.js'
    import {Stomp} from '../../js/stomp.min.js'
	export default {
        
		data() {
			return {
				title: 'Hello',
				CustomBar: this.CustomBar,
				control:0,
				url1:'http://47.103.64.35:8994/GetConsult?pageNum=1&pageLimit=8&type="1"',
				url2:'http://47.103.64.35:8994/GetConsult?pageNum=1&pageLimit=8&type="0"',
                getdate:0,
                getrecommend:0,
                animationData:{},
                backNum : 1
			}
		},
		components:{
			date,
			recommend,
			attention
		}
		,
		onLoad() {
         
               
		},
        onBackPress(){
            
            let c =this;
            
            if(c.backNum == 1){
                  
                  let userInfos = uni.getStorageSync("userInfos");
                         
                         let {userId,token} = userInfos  
                          console.log(userId);
                           
                            
                        var url = "wss://47.103.64.35:8996/websocket"
                   
                   //       var socket = new SockJS(url);
                         var  stompClient = Stomp.client(url);
                         
                         console.log(stompClient);
                       
                   
             
                  let connect = uni.getStorageSync("hasconnect");
                    console.log(connect);
                   uni.removeStorageSync("hasconnect");
                  //断开连接的方法
                  if (stompClient !== undefined && connect =="done") {
                        
                  
                      
                      var headers ={
                          userId:userId
                      }
                               
                      stompClient.disconnect(headers,function(){
                          console.log("发送成功");
                          
                      });
                      
                      console.log("Disconnected");
                  }else{
                      console.log("当前没有连接websocket")
                  }
                  
                  
                plus.nativeUI.toast('再次按下返回键退出');
                     c.backNum++;
              }
              
              
            if(c.backNum == 2){
                                   
                   setTimeout(function(){
                 
                       plus.runtime.quit();
                           
              },1500)  
                }
            
            
            
            },
		methods: {
            search(){
				// uni.navigateTo({
				// 	url:"../search/search",
				// 	success() {
				// 		console.log("跳转成功")
				// 	}
				// })
			},
			change(data){
				console.log(data)
				this.control = data;
			},
              reachdate(){
                this.getdate = 0;
            },
          reachrecommend(){
              this.getrecommend = 0;
          }
            
		},
		mounted() {
			
            // 进行推荐和最新组件的切换
			  uni.$on('update',data =>{
	    console.log('监听到事件来自 update ，携带参数 msg 为：' + data.msg);
		this.change(data.msg)
	})
    
    // 预存定计划的数据
    this.$api.test({
        url:"setgoals/getMygoals?type=0&PageNum=1&PageLimit=5"
    }).then((res) =>{
        for(let i of res.data.plan){
            
            let {signednum,signnum} = i
            
            signnum = parseFloat(signnum); 
         signednum = parseFloat(signednum); 
            
            i.progress = Math.round(signednum / signnum * 10000) / 100.00 + "%";
            
        }
        
          uni.setStorage({
              key:"ongoingfirst",
              data:res.data,
              success() {
                  console.log("预存成功")
              }
          }) 
    })
    .catch((err) =>{
        
        
        
    })
        
    
    
    
    
     //   uni.request({
     //     url:"http://07cr7.live:8994/getMygoals?type=0&PageNum=1&PageLimit=5",
     //     method:"GET",
     //     success(res) {
         
     //       for(let i of res.data.plan){
             
     //         let {signednum,signnum} = i
             
     //         signnum = parseFloat(signnum); 
     //      signednum = parseFloat(signednum); 
             
     //         i.progress = Math.round(signednum / signnum * 10000) / 100.00 + "%";
             
     //     }
         
     //       uni.setStorage({
     //           key:"ongoingfirst",
     //           data:res.data,
     //           success() {
     //               console.log("预存成功")
     //           }
     //       })
          
     //     },
     //     fail() {
         	
     //     }
     // });
    
    
      
		},
		onPullDownRefresh() {
		
        //下拉刷新
        
			setTimeout(function() {
				uni.$emit('data',{msg:'获取数据'})
				uni.stopPullDownRefresh();
				console.log("刷新结束")
			}, 1000);
		},
        onReachBottom() {
            
            //上拉加载
            
            if(this.control == 0){
                  this.getdate = 1;
            }
          if(this.control == 1){
               this.getrecommend = 1;
          }
           
        },
        onShow() {
            
            
            //进行网络连接的判断
            
            uni.onNetworkStatusChange(function (res) {
    console.log(res.isConnected);
    console.log(res.networkType);
    
    if(res.isConnected == false){
 uni.showToast({
    title: '网络连接不可用',
    duration: 2000,
    icon:"none",
    position:"center"
    
});
    }
    
});


    var animation = uni.createAnimation({
      duration: 400,
        timingFunction: 'ease',
    })

    this.animation = animation

    animation.translate(2,2).step()

    this.animationData = animation.export()



        }
		}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		font-size: 16px;
		text-align: center;
	}
 .show{
     display: none;
 }

</style>
